//
//  ShoppingCarCell.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/8/17.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit




class ShoppingCarCell: BaseTableViewCell {
    
    weak var delegate : ShoppingCarCellDelegate?{
        
        didSet{
            
            if delegate == nil {
                return
            }
            
            self.VC = delegate as? ShoppingCarController
            
        }
        
    }
    
    weak var VC : ShoppingCarController?

    @IBOutlet weak var selecteBtn: UIButton!
    
    @IBOutlet weak var leftImageView: UIImageView!
    
    @IBOutlet weak var nameLabel: UILabel!
    
    @IBOutlet weak var priceLabel: UILabel!
    
    @IBOutlet weak var goodsCountLabel: UILabel!
    
    @IBOutlet weak var topLineView: UIView!
    
    @IBOutlet weak var deleBtn: UIButton!
    
    @IBOutlet weak var bgView: UIView!

    @IBOutlet weak var bottomMargin: NSLayoutConstraint!
    
    @IBOutlet weak var bottomMarginView: UIView!
    
    @IBOutlet weak var subBtn: UIButton!
    @IBOutlet weak var addBtn: UIButton!
    @IBOutlet weak var bgViewHstr: NSLayoutConstraint!
    var isLastRow = true{
    
        didSet{
        
            if isLastRow == true {
                
                bgViewHstr.constant = 102
            } else {
                
                bgViewHstr.constant = 92.5
                
            }

        
        }
    
    }
    
    var index : IndexPath?
    
    
    var model : HomeItemModel?{
    
    
        didSet{
        
            if model == nil || index == nil{
                return
            }
        
            if index!.row == 0 {
                topLineView.isHidden = true
            } else {
                topLineView.isHidden = false

            }
            
            leftImageView.sd_setImage(with: URL(string: model!.goods_image_url), placeholderImage: UIImage.defaulImage(), options: []) { (image, _, _, _) in
                
            }

            
            nameLabel.text = model?.goods_name
            
            priceLabel.text = "¥" + model!.goods_price
            
            selecteBtn.isSelected = model!.isSelect
            
            
            let goodsCount = Int(model!.goods_num)
            
            if goodsCount != nil {
                
                goodsCountLabel.text = model?.goods_num
                
                subBtn.isEnabled = goodsCount! > 1
                
                addBtn.isEnabled = goodsCount! < 99
                
                if goodsCount! > 99 {
                    
                    model?.goods_num = "99"
                    
                    goodsCountLabel.text = "99"
                    
                }
            }
            
        
        }
    
    
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        bottomMarginView.backgroundColor = UIColor.viewBgGrey()
        selecteBtn.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10)
        leftImageView.addCornerAndBorder(borderColor: UIColor.lineGray(), borderWidth: 0.5, masksToBounds: true, cornerRadius: 5)
        
        bgView.addCornerAndBorder(borderColor: UIColor.lineGray(), borderWidth: 1, masksToBounds: true, cornerRadius: 3)
        
        bgView.backgroundColor = UIColor.lineGray()
        
        addBtn.setTitleColor(UIColor.themeColor(), for: .normal)
        addBtn.setTitleColor(UIColor.lightGray, for: .disabled)
        addBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 5, 0)
        addBtn.setBackgroundImage(UIImage.imageWithColor(color: UIColor.colorWith(r: 234, g: 234, b: 234)), for: .disabled)
        addBtn.setBackgroundImage(UIImage.imageWithColor(color: UIColor.white), for: .normal)
        
        subBtn.setTitleColor(UIColor.themeColor(), for: .normal)
        subBtn.setTitleColor(UIColor.lightGray, for: .disabled)
        subBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 2, 0)
        subBtn.setBackgroundImage(UIImage.imageWithColor(color: UIColor.colorWith(r: 234, g: 234, b: 234)), for: .disabled)
        subBtn.setBackgroundImage(UIImage.imageWithColor(color: UIColor.white), for: .normal)
        

    }
    

    @IBAction func addBtnDidClick(_ sender: UIButton) {
        
        var goodCount = Int(model!.goods_num)!
        
        goodCount += 1
        
        sender.isEnabled = goodCount < 99
        
        subBtn.isEnabled = goodCount > 1
        
        if goodCount > 99 {
            
            model?.goods_num = "99"
            
            goodsCountLabel.text = "99"
            
        }else{
        
            model?.goods_num = "\(goodCount)"
            
            goodsCountLabel.text = "\(goodCount)"

        
        }

        VC?.calculateTotalPrice()
        
 
    }
    
    
    
    @IBAction func subBtnDidClick(_ sender: UIButton) {
        
        var goodCount = Int(model!.goods_num)!
        
        goodCount -= 1
        
        addBtn.isEnabled = goodCount < 99
        
        sender.isEnabled = goodCount > 1

        
        model?.goods_num = "\(goodCount)"
        
        goodsCountLabel.text = "\(goodCount)"

        
        
        VC?.calculateTotalPrice()
        
        
    }
    
    
    @IBAction func deleBtnDidClick(_ sender: UIButton) {
        
        let tempVC = UIAlertController(title: nil, message: "确定删除此商品吗?", preferredStyle: .alert)
        
        weak var weakSelf = self
        let action1 = UIAlertAction(title: "确定", style: .destructive) { (action) in
            
            let model = weakSelf!.VC!.shopCartList[weakSelf!.index!.section]
            
            model.goods.remove(at: weakSelf!.index!.row)
            
            
            if model.goods.count < 1{
                
                weakSelf!.VC!.shopCartList.remove(at: weakSelf!.index!.section)
                
            }
            
            if weakSelf!.VC!.shopCartList.count < 1{
                
                weakSelf!.VC!.bottomView.isHidden = true
                
                
                if weakSelf!.VC!.hotRecommList.count < 1{
                    
                    weakSelf!.VC!.nothingRootView.bottomLabel.attributedText = weakSelf!.VC!.attributedString(str: "亲!您的购物车还是\n空的哟~", font: UIFont.systemFont(ofSize: 15))
                    
                    weakSelf!.VC!.nothingRootView.isHidden = false
                    
                    
                }else{
                    
                    let inset = weakSelf!.VC!.plainTableView.contentInset
                    
                    let inset1 = UIEdgeInsetsMake(inset.top, inset.left, inset.bottom - 49, inset.right)
                    
                    weakSelf!.VC!.plainTableView.contentInset = inset1
                    
                    
                }
                
            }
            
            
            weakSelf?.delegate?.calculateTotalPrice()
            
            
            weakSelf!.VC!.plainTableView.reloadData()
            
            if weakSelf!.VC!.hotRecommList.count > 0{
                
                weakSelf!.VC!.plainTableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: false)
                
                
            }
            
            
            
            
        }
        
        
        
        let action2 = UIAlertAction(title: "取消", style: .default) { (action) in
            
            
            
        }
        
        tempVC.addAction(action2)
        tempVC.addAction(action1)
        
        VC?.present(tempVC, animated: true, completion: nil)
        
        
        
    }
    
    @IBAction func selecteBtnDidTap(_ sender: UIButton) {
        
        sender.isSelected = !sender.isSelected
        
        model?.isSelect = sender.isSelected
        
        VC?.calculateTotalPrice()
        
        VC?.plainTableView.reloadData()
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
